from django.shortcuts import render, redirect

# Create your views here.
from django.urls import reverse

from nav.models import Nav_bar, Role_nav, Left_bar

site={"name":"AI油站导航"}
def index(request):
    role_nav = Role_nav.objects.first()
    return nav(request,code=role_nav.id)

def nav(request,code,navId=None):
    # 角色
    role_navs= Role_nav.objects.filter(id=code)
    left_bars, nav_bars,solinks = [], [],[]
    left_bar_name = "目录"

    role_nav=None
    if len(role_navs)>0:
        role_nav =role_navs[0]
        solinks = role_nav.solink_set.all().order_by('sort')

        nav_bars = role_nav.nav_bar_set.all()
        nav_bar = None if len(nav_bars)<1 else nav_bars[0]
        if navId!=None:
            nav_bar = role_nav.nav_bar_set.filter(id=navId).first()
        left_bar_name="导航"
        if nav_bar is not None:
            left_bar_name =nav_bar.title
            left_bars = Left_bar.objects.filter(nav_bar_id=nav_bar.id).all().order_by('sort')
        #     if len(nav_bars) > 0:
        #         nav_bar = nav_bars[0]

        #         left_bar_name = nav_bar.title
    return render(request, "index.html",
                  {"site": site, "role_nav":role_nav,"nav_bars": nav_bars,"left_bars": left_bars, "left_bar_name": left_bar_name,"solinks":solinks})